<?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Crm_Contract extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;

    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'crm_contract';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'agencyid',
        'oid',
        'contract_sn',
        'type',
        'mid',
        'total',
        'days',
        'signdate',
        'startdate',
        'enddate',
        'minmeets',
        'preview_url',
        'legal_name',
        'legal_signurl',
        'legal_status',
        'user_name',
        'user_signurl',
        'user_status',
        'status',
        'option_id',
        'option_name'
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function _generateSn($prefix = 'HT')
    {
        list($usec, $sec) = explode(' ', microtime());
        return $prefix . date('YmdHis', $sec) . substr($usec, 3, 2);
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){
            $query->where('agencyid', $filter['aid']);
        })->orderBy('id', 'desc');
    }

}
